﻿using Microsoft.Xna.Framework;

namespace GameName1
{
    class GameStateSpawningAngryAliens : GameState
    {
        private const double SpawnDelay = 30;
        private double _lastSpawnTime;        
        private int _spawnedObjectsCount;

        public GameStateSpawningAngryAliens()
        {
            GameObjectsManager.GetInstance().DestroyAllObjectsOfType<Projectile>();
            GameObjectsManager.GetInstance().DestroyAllObjectsOfType<AngryAlienExplosion>();

            if(AngryAlienController.MoveDelay> 250) AngryAlienController.MoveDelay -= 50;
            if(AngryAlien.FireRate > 100) AngryAlien.FireRate -= 50;
        }

        public override void Update(GameplayScene gameplayScreen, GameTime gameTime)
        {
            if (gameTime.TotalGameTime.TotalMilliseconds > _lastSpawnTime + SpawnDelay)
            {               
                SpawnAngryAlien(_spawnedObjectsCount);
                _spawnedObjectsCount++;
                
                _lastSpawnTime = gameTime.TotalGameTime.TotalMilliseconds;
            }
            if(_spawnedObjectsCount==55) GameplayScene.GameState = new GameStateRunning();       
        }

        private void SpawnAngryAlien(int i)
        {
            var position = new Vector2(35 + (i % 11) * 50, 70 + (i / 11) * 45);
            var angryAlien = new AngryAlien(position, i);

            Weapon weapon = new RocketLauncher(1);

            angryAlien.Weapon = weapon;
            GameObjectsManager.GetInstance().Instantiate(angryAlien);
        }
    }
}
